import { renderWithHooks } from "../react/ReactHooks";
import { updateFiberNode } from "../shared/utils";
import { reconcileChildren } from "./reconcileChildren";
import { IFiber } from "./updateContainer";

/**
 * 处理原生元素 div span
 */
export function updateHostComponent(wip: IFiber) {
  if (!wip.stateNode) {
    // 创建dom对象
    wip.stateNode = document.createElement(wip.type);

    // 更新dom对象的属性
    updateFiberNode(wip.stateNode, {}, wip.props);
  }
  // 协调子元素
  reconcileChildren(wip, wip.props.children);
  
}

/**
 * 处理文本元素
 */
export function updateHostText(wip: IFiber) {
  wip.stateNode = document.createTextNode(wip.props.children);
}

/**
 * 处理函数组件
 */
export function updateFunctionCompoent(wip: IFiber) {
  renderWithHooks(wip);

  const { type, props } = wip;
  const vnode = type(props);

  reconcileChildren(wip, vnode);
}

/**
 * 处理类组件
 */
export function updateClassComponent(wip: IFiber) {
  const { type, props } = wip;

  const vnode = new type(props).render();
  reconcileChildren(wip, vnode);
}
